import os
import django

# 初始化django配置，注意修改blog_manager为项目名
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blog_manager.settings")
django.setup()

# 以下代码导入模型，根据情况修改
from post.models import *

# 清空数据
Role.objects.all().delete()
User.objects.all().delete()

role1 = Role.objects.create(id=1, name="超级管理员角色")
role2 = Role.objects.create(id=2, name="管理员角色")
role3 = Role.objects.create(id=3, name="教师角色")
role4 = Role.objects.create(id=4, name="学生角色")

user0 = User.objects.create_superuser(id=1, username="admin", password="admin", email="admin@qq.com")

user1 = User.objects.create_user(id=2, username="张三(超级管理员)", password="zhansan", email="zs@qq.com")
user2 = User.objects.create_user(id=3, username="李四(管理员)", password="lisi", email="ls@qq.com")
user3 = User.objects.create_user(id=4, username="王五(管理员)", password="wangwu", email="ww@qq.com")
user4 = User.objects.create_user(id=5, username="Tom(学生助理)", password="Tom", email="tom@qq.com")
user5 = User.objects.create_user(id=6, username="Jerry(学生)", password="Jerry", email="jerry@qq.com")
user6 = User.objects.create_user(id=7, username="John(学生)", password="John", email="john@qq.com")

from django.db import connection

# TODO 创建模型对象 多对多关系

# 超级管理员角色关联用户
role1.users.add(user1)
# role1.users.set([user1])  # 覆盖

# 管理员角色关联用户
role2.users.add(user2, user4)

# 教师角色关联用户
role3.users.add(user1, user3)

# 学生角色关联用户
role4.users.add(user4, user5, user6)

for sql in connection.queries:
    print(sql["sql"])
